﻿using System;
using System.Collections.Generic;
using System.Linq;
using NUnit.Framework;

namespace MailChimpAmazon.SES.Tests
{
	[TestFixture]
	class Tests
	{
		private readonly SesApi _mApi;
		private const String ApiKey = "Todo: Insert MailChimp API key";
		private const String EmailSender = "Todo: Insert sender email address, must be verified eg noreply@nogginbox.co.uk";
		private const String EmailRecipient = "Todo: Insert recipient email address eg noreply@nogginbox.co.uk";
		private const String EmailBadRecipient = "skyroger5@sky.com";

		public Tests()
		{
			_mApi = new SesApi(ApiKey);
		}

		[Test]
		public void TestGetQuota()
		{
			var result = _mApi.GetSendQuota();
			Console.WriteLine("GetQuota result: " + result);

			Assert.AreEqual(10000, result.Max24HourSend);
			Assert.AreEqual(5, result.MaxSendRate);
			Assert.True(result.SentLast24Hours > 0);
		}

		[Test]
		public void TestGetSendStatistics()
		{
			var result = _mApi.GetSendStatistics().ToList();

			Console.WriteLine("GetSendStatistics result: {" + String.Join("}, {", result) + "}");

			var result1 = result.ToArray()[0];
			Assert.NotNull(result1);
			Assert.AreEqual(0, result1.Complaints);
		}


		[Test]
		public void TestListVerifiedEmailAddresses()
		{
			var result = _mApi.ListVerifiedEmailAddresses();

			Console.WriteLine("ListVerifiedEmailAddresses result: " + result);

			Assert.True(result.email_addresses.Contains(EmailSender));
			Assert.True(result.email_addresses.Count() >= 1);
		}

		[Test]
		public void TestSendEmail()
		{
			var result =
			_mApi.SendEmail("Test email from MailChimpAmazon.SES.Tests",
							"<h1>HTML email</h1><p>Html version to follow</p>",
							"Not HTML",
							new EmailAddress("Test sender", EmailSender),
							new List<EmailAddress> { new EmailAddress("Richard", EmailRecipient) },
							tags: new List<String> { "test", "test2" }
			);

			Console.WriteLine("Send email result: " + result);

			Assert.NotNull(result.message_id);
			Assert.AreEqual("sent", result.status);
		}

		[Test]
		public void TestSendEmailToBadAddress()
		{
			var result =
			_mApi.SendEmail("Test email from MailChimpAmazon.SES.Tests",
							"<h1>HTML email</h1><p>Html version to follow</p>",
							"Not HTML",
							new EmailAddress("Test sender", EmailSender),
							new List<EmailAddress> { new EmailAddress("Bad email", EmailBadRecipient) },
							tags: new List<String> { "test", "test2" }
			);

			Console.WriteLine("Send email result: " + result);

			Assert.Null(result.message_id);
			Assert.AreEqual("rejected", result.status);
		}

		[Test]
		[Ignore("Don't want to verify lots of members all the time")]
		public void TestAddNewVerifiedEmailAddress()
		{
			_mApi.VerifyEmailAddress(EmailSender);
		}
	}
}
